package com.haiwen.boot.service.lmpl;
import com.github.pagehelper.PageHelper;
import com.haiwen.boot.mapper.BlogMapper;
import com.haiwen.boot.mapper.PoetryMapper;
import com.haiwen.boot.pojo.BlogWithBLOBs;
import com.haiwen.boot.pojo.Poetry;
import com.haiwen.boot.service.HomePageService;
import com.haiwen.boot.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
class homePageServiceImpl implements HomePageService {
    @Autowired
    private PoetryMapper mapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private BlogMapper blogMapper;
    @Override
    public Poetry randomPoetry() {
       // redisTemplate.setKeySerializer(new StringRedisSerializer());
        Poetry poetry=(Poetry)redisUtil.get("poetry");
        if (poetry==null){
             poetry = mapper.randomPoetry();
            redisUtil.set("poetry",poetry,30);
        }
        return poetry;
    }
    @Override
    public List<BlogWithBLOBs> blogShow(int pageNum, int pageSize, Boolean isShow) {
        PageHelper.startPage(pageNum, pageSize);
        String regEx="[\n`~!@#$%^&*()+=|{}]"; //正则表达，去除文本中的特殊字符
        String nullStr = " ";
       /* List<BlogWithBLOBs>  blogWithBLOBs;*/
       /* redisTemplate.setKeySerializer(new StringRedisSerializer());
         blogWithBLOBs = (List<BlogWithBLOBs>) redisTemplate.opsForList().leftPop("blogList");*/
        /*if (blogWithBLOBs==null){*/
        List<BlogWithBLOBs>  blogWithBLOBs = blogMapper.blogShow(isShow);
          /*  redisTemplate.opsForList().leftPush("blogList",blogWithBLOBs);
            redisTemplate.expire("alluser", 60, TimeUnit.SECONDS);*/
     /*   }*/
        for (BlogWithBLOBs blog:blogWithBLOBs) {
            String substring = blog.getReleaseTime().substring(0,10);
            String newString = blog.getBlContent().replaceAll(regEx, nullStr);
            String listBlogContent=newString.length()<50?newString.substring(0,newString.length()):newString.substring(0,130)+"……";
            blog.setBlContent(listBlogContent);
            blog.setReleaseTime(substring);
        }
        return blogWithBLOBs;
    }
}
